package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.common.StringUtil;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.service.ErTongBJKService;
import com.jy.rhin.infra.cda.service.XinShengErFSService;
import com.jy.rhin.infra.cda.xinshenger.ErTongBJKVo;
import com.jy.rhin.infra.cda.xinshenger.XinShengEJTFSVo;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.XinShengErJTFS;
import com.jy.rhin.model.body.xinshenger.*;
import com.jy.rhin.model.header.Author;
import com.jy.rhin.model.header.Custodian;
import com.jy.rhin.model.header.Patient;
import com.jy.rhin.model.header.PatientRole;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class XinShengErFSApplication {

    @Resource
    private XinShengErFSService xinShengErFSService;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private CDACreator cdaCreator;
    @Resource
    private ErTongBJKService erTongBJKService;

    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String cHANHOU42TJKJCJLBH,String fUWUJGBH) {
        return cdaCreator.create(createFS(cHANHOU42TJKJCJLBH,fUWUJGBH), "03xinshengerfs.ftl");
    }

    public ClinicalDocumentData createFS(String cHANHOU42TJKJCJLBH,String fUWUJGBH) {
        XinShengEJTFSVo info = xinShengErFSService.queryOne(fUWUJGBH, cHANHOU42TJKJCJLBH);
        if(Objects.isNull(info)){
            throw new CDAException("主业务数据不存在",null);
        }
        HospitalDTO hospital = hospitalAPI.info(info.getFUWUJGBH()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(info.getFUWUJGBH()).getData();
        if(Objects.isNull(hospital)){
            throw new CDAException("机构数据不存在",null);
        }

        XinShengErJTFS docData = new XinShengErJTFS();
        setClinicalDocumentData(docData, info, hospital);
        
        docData.setId(info.getId()+"");
        docData.setXinShengE(createXinShengE(info));
        docData.setJianKangWT(createJianKangWT(info));
        docData.setEr(createEr(info));
        docData.setShiYanJC(createShiYanJC(info));
        docData.setShengMingTZ(createShengMingTZ(info));
        docData.setPiFu(createPiFu(info));
        docData.setYiBanWG(createYiBanWG(info));
        docData.setTouBu(createTouBu(info));
        docData.setYanBu(createYanBu(info));
        docData.setSiZhi(createSiZhi(info));
        docData.setJinBu(createJinBu(info));
        docData.setBi(createBi(info));
        docData.setKouQiangYanHouYaChiJC(createKouQiangYanHouYaChiJC(info));
        docData.setZhiChang(createZhiChang(info));
        docData.setXingZhang(createXingZhang(info));
        docData.setHuXiXT(createhuXiXt(info));
        docData.setShengZhiQi(createShengZhiQi(info));
        docData.setFuBu(createFuBu(info));
        docData.setJiZhu(createJiZhu(info));
        docData.setJianKangZD(createJianKangZD(info));
        docData.setZhuanZhenJY(createZhuanZhenJY(info));
        docData.setXiaCiSFAP(createXiaCiSF(info));

        return docData;
    }

    private JiZhu createJiZhu(XinShengEJTFSVo info) {
        JiZhu jiZhu = new JiZhu();
        jiZhu.setJiZhuJCYCBZ("2".equals(info.getJIZHUYCBZ())? "true":"false");
        jiZhu.setJiZhuJCYCJGMS(StringUtil.isEmpty(info.getJIZHUYCXS())? "-":info.getJIZHUYCXS());
        return jiZhu;
    }

    private FuBu createFuBu(XinShengEJTFSVo info) {
        FuBu fuBu = new FuBu();
        fuBu.setFuBuJCYCBZ("2".equals(info.getFUBUCZYCBZ())? "true":"false");
        fuBu.setShengZhiQJCYCJGMS(StringUtil.isEmpty(info.getFUBUCZYCXS())? "-":info.getFUBUCZYCXS());
        //todo 缺失
        fuBu.setQiDaiTLBZ("false");
        fuBu.setQiDaiJCJG(new CodeObject(info.getQIDAIJCJGDM(), info.getQIDAIJCJGMC()));
        return fuBu;
    }

    private HuXiXT createhuXiXt(XinShengEJTFSVo info) {
        HuXiXT huXiXT = new HuXiXT();
        huXiXT.setFeiBuTZYCBZ("2".equals(info.getXINFEITZYCBZ())? "true":"false");
        huXiXT.setFeiBuTZYCJGMS(StringUtil.isEmpty(info.getXINFEITZYCXS())? "-":info.getXINFEITZYCXS());
        return huXiXT;
    }

    private XingZhang createXingZhang(XinShengEJTFSVo info) {
        XingZhang xingZhang = new XingZhang();
        //todo 心脏的先空着
        /*xingZhang.setXingZhangTZYCBZ("2".equals(info.getXINFEITZYCBZ())? "true":"false");
        xingZhang.setXingZhangTZYCJGMS(StringUtil.isEmpty(info.getXINFEITZYCXS())? "-":info.getXINFEITZYCXS());*/
        xingZhang.setXingZhangTZYCBZ("true");
        xingZhang.setXingZhangTZYCJGMS("-");
        return xingZhang;
    }

    private ZhiChang createZhiChang(XinShengEJTFSVo info) {
        ZhiChang zhiChang = new ZhiChang();
        zhiChang.setGangMenJCYCBZ("2".equals(info.getGANGMENYCBZ())? "true":"false");
        zhiChang.setGangMenJCYCJGMS(StringUtil.isEmpty(info.getGANGMENYCXS())? "-":info.getGANGMENYCXS());
        return zhiChang;
    }

    private KouQiangYanHouYaChiJC createKouQiangYanHouYaChiJC(XinShengEJTFSVo info) {
        KouQiangYanHouYaChiJC kouQiangYanHouYaChiJC = new KouQiangYanHouYaChiJC();
        kouQiangYanHouYaChiJC.setKouQiangJCYCBZ("2".equals(info.getKOUQIANGYCBZ())? "true":"false");
        kouQiangYanHouYaChiJC.setKouQiangJCYCJGMS(StringUtil.isEmpty(info.getKOUQIANGYCXS())? "-":info.getKOUQIANGYCXS());
        return kouQiangYanHouYaChiJC;
    }

    private Bi createBi(XinShengEJTFSVo info) {
        Bi bi = new Bi();
        bi.setBiJianCYCJGMS(StringUtil.isEmpty(info.getBIYICXS())? "-":info.getBIYICXS());
        bi.setBiJianCYWBZ("2".equals("info.getBIYICBZ()")? "true":"false");
        return bi;
    }

    private JinBu createJinBu(XinShengEJTFSVo info) {
        JinBu jinBu = new JinBu();
        jinBu.setJinBuBKBZ("2".equals(info.getJINBUBKNZ())? "true":"false");
        jinBu.setJinBuBKJCJGMS(StringUtil.isEmpty(info.getJINBUBKXS())? "-":info.getJINBUBKXS());
        return jinBu;
    }

    private SiZhi createSiZhi(XinShengEJTFSVo info) {
        SiZhi siZhi = new SiZhi();
        siZhi.setSiZhiHDDYCBZ("2".equals(info.getSIZHIHDDYCBZ())? "true":"false");
        siZhi.setSiZhiHDDYCJGMS(StringUtil.isEmpty(info.getSIZHIHDDYCXS())? "-":info.getSIZHIHDDYCXS());
        return siZhi;
    }

    private YanBu createYanBu(XinShengEJTFSVo info) {
        YanBu yanBu = new YanBu();
        yanBu.setYanWaiGJCYCBZ("2".equals(info.getYANWAIGYCBZ())? "true":"false");
        yanBu.setYanWaiGJCYCJGMS(info.getYANWAIGYCXS());
        return yanBu;
    }

    private TouBu createTouBu(XinShengEJTFSVo info) {
        TouBu touBu = new TouBu();
        //todo 缺失需要增加字段
        touBu.setQianLuBHBZ("false");
        touBu.setQianLuHJ(null != info.getQIANXINHJ()? info.getQIANXINHJ().setScale(1, RoundingMode.HALF_UP).toString():"");
        touBu.setQianLuZJ(null != info.getQIANXINZJ()? info.getQIANXINZJ().setScale(1, RoundingMode.HALF_UP).toString():"");
        touBu.setQianLuZL(new CodeObject(null != info.getQIANXINZLDM()? String.valueOf(info.getQIANXINZLDM()):"", info.getQIANXINZLMC()));
        return touBu;
    }

    private YiBanWG createYiBanWG(XinShengEJTFSVo info) {
        YiBanWG yiBanWG = new YiBanWG();
        yiBanWG.setXinShengEMS(new CodeObject(info.getMIANSEDM(), info.getMIANSEMC()));
        yiBanWG.setXinShengEJXBZ("1".equals(info.getCHUSHENJXBZ())?"true":"false");
        yiBanWG.setXinShengEJXMS(info.getCHUSHENJXMC());
        return yiBanWG;

    }

    private PiFu createPiFu(XinShengEJTFSVo info) {
        PiFu pifu = new PiFu();
        pifu.setPiFuJCJG(new CodeObject(shiPeiPiFuJCJGDM(info.getPIFUJCJGDM()), info.getPIFUJCJGMC()));
        pifu.setHuangDanBW(new CodeObject(info.getHUANGDANBWDM(), info.getHUANGDANBWMC()));
        return pifu;
    }
    private String shiPeiPiFuJCJGDM(String piFuJCJGDM) {
        if (!StringUtil.isEmpty(piFuJCJGDM)) {
            Integer integer = Integer.valueOf(piFuJCJGDM);
            if (integer < 10) return "0" + integer;
            return piFuJCJGDM;
        } else {
            return piFuJCJGDM;
        }
    }

    private ShiYanJC createShiYanJC(XinShengEJTFSVo info) {
        ShiYanJC shiYanJC = new ShiYanJC();
        shiYanJC.setXinShengEJBSX(new CodeObject(info.getXINSHENGEJBSCQKDM(), info.getXINSHENGEJBSCQKMC()));
        return shiYanJC;
    }

    private String shiPeiJiBingSCQKMC(String jiBingSCQKDM){
        String jiBingSCQKMC = "其他";
        switch (jiBingSCQKDM) {
            case "1":
                jiBingSCQKMC = "先天性甲状腺功能减低症";
                break;
            case "2":
                jiBingSCQKMC = "苯丙酮尿症";
                break;
            case "3":
                jiBingSCQKMC = "6-磷酸葡萄糖脱氢酶缺乏症";
                break;
            case "4":
                jiBingSCQKMC = "先天性肾上腺皮质增生症";
                break;
            case "5":
                jiBingSCQKMC = "半乳糖血症";
                break;
            case "6":
                jiBingSCQKMC = "地中海贫血";
                break;
            case "9":
                jiBingSCQKMC = "其他";
                break;
        }
        return jiBingSCQKMC;
    }

    private Er createEr(XinShengEJTFSVo info) {
        Er er = new Er();
        er.setErWaiJCYCBZ("2".equals(info.getERWAIGYCBZ())? "true":"false");
        er.setErWaiJCYCJGMS(StringUtil.isEmpty(info.getERWAIGYCXS())? "-" : info.getERWAIGYCXS());
        er.setXinShengRTLSX(new CodeObject(info.getXINSHENGETLSCJG(), "1".equals(info.getXINSHENGETLSCJG())? "通过" :"未通过"));
        return er;
    }

    private Author createChuangJianZ(XinShengEJTFSVo info , HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(null != info.getSUIFANGRQ() ? CDAUtil.toD8(info.getSUIFANGRQ()) : "");
        author.setName(info.getSUIFANGYSXM());
        author.setOrganizationName(info.getSUIFANGDWMC());
        author.setOrganizationAddr(hospital.getAddress());
        author.setOrganizationId(hospital.getOutId());
        author.setId(info.getSUIFANGYSGH());
        return author;
    }

    private XinShengE createXinShengE(XinShengEJTFSVo info){
        ErTongBJKVo erTongBJKVo = erTongBJKService.queryOne(info.getFUWUJGBH(), info.getERTONGWYBS());
        XinShengE data = new XinShengE();
        data.setJianKangDABSH(shiPeiJiangKangDABSH(info.getGERENDABS()));
        data.setDiZhi(null == erTongBJKVo ? "-": shiFouYouZhi(erTongBJKVo.getXIANZHUDZQTXX()));
        data.setJieDao(null == erTongBJKVo ? "-": shiFouYouZhi(erTongBJKVo.getXIANZHUDZCJMC()));
        data.setXiangZhen(null == erTongBJKVo ? "-": shiFouYouZhi(erTongBJKVo.getXIANZHUDZXZMC()));
        data.setQu(null == erTongBJKVo ? "-": shiFouYouZhi(erTongBJKVo.getXIANZHUDZXQMC()));
        data.setShi(info.getJIATINGDZSDMC());
        data.setSheng(info.getJIATINGDZSFMC());
        data.setYouZhengBM(shiFouYouZhi(info.getJIATINDZYZBM()));
        data.setZhengJianHM(info.getSHENFENZH());
        data.setXingMing(info.getERTONGXM());
        data.setXingBie(new CodeObject(null == erTongBJKVo ? "9": shiFouYouZhi(erTongBJKVo.getERTONGXB()),
                null == erTongBJKVo ? "未说明的性别":
                "0".equals(shiFouYouZhi(erTongBJKVo.getERTONGXB()))? "未知的性别":
                        "1".equals(shiFouYouZhi(erTongBJKVo.getERTONGXB()))? "男性":
                                "2".equals(shiFouYouZhi(erTongBJKVo.getERTONGXB()))? "女性":
                                        "9".equals(shiFouYouZhi(erTongBJKVo.getERTONGXB()))? "未说明的性别":""));
        data.setChuShengRQ(CDAUtil.toD8(info.getCHUSHENGRQ()));
        List<JianHuR> jianHuRs = new ArrayList<>();
        JianHuR fuQing = new JianHuR();
        fuQing.setCodeObject(new CodeObject("51", "父亲"));
        fuQing.setJianHuRDH(shiFouYouZhi(info.getFUQINDHHM()));
        fuQing.setJianHuRXM(shiFouYouZhi(info.getFUQINXM()));
        fuQing.setJianHuRCSRQ(CDAUtil.toD8(info.getFUQINCSRQ()));
        fuQing.setJianHuRZY(new CodeObject(zhiYeDM2261To6565(shiFouYouZhi(info.getFUQINZYDM())),zhiYeMC2261To6565(shiFouYouZhi(info.getFUQINZYDM()))));
        jianHuRs.add(fuQing);
        JianHuR MuQing = new JianHuR();
        MuQing.setCodeObject(new CodeObject("52", "母亲"));
        MuQing.setJianHuRDH(shiFouYouZhi(info.getMUQINDHHM()));
        MuQing.setJianHuRXM(shiFouYouZhi(info.getMUQINXM()));
        MuQing.setJianHuRCSRQ(CDAUtil.toD8(info.getMUQINCSRQ()));
        MuQing.setJianHuRZY(new CodeObject(zhiYeDM2261To6565(shiFouYouZhi(info.getMUQINZYDM())),zhiYeMC2261To6565(shiFouYouZhi(info.getMUQINZYDM()))));
        jianHuRs.add(MuQing);
        data.setJianHuR(jianHuRs);
    	return data;
    }

    private String shiPeiJiangKangDABSH(String gerendabs) {
        if (null != gerendabs && gerendabs.length() < 17) {
            gerendabs = String.format("%-17s", gerendabs).replace(' ', '0');
        }
        return gerendabs;
    }

    private String zhiYeMC2261To6565(String daiMa) {
        String zhiYeMC = "不便分类的其他从业人员";
        switch (daiMa) {
            case "11": //国家公务员
                zhiYeMC = "国家机关、党群组织、企业、事业单位负责人";
                break;
            case "13": //专业技术人员
                zhiYeMC = "专业技术人员";
                break;
            case "17": //职员
                zhiYeMC = "办事人员和有关人员";
                break;
            case "21": //企业管理人员
                zhiYeMC = "企业负贵人";
                break;
            case "24": //工人
                zhiYeMC = "生产、运输设备操作人员及有关人员";
                break;
            case "27": //农民
                zhiYeMC = "农、林、牧、渔、水利业生产人员";
                break;
            case "37": //现役军人
                zhiYeMC = "军人";
                break;
            case "31": //学生
            case "51": //自由职业者
            case "54": //个体经营者
            case "70": //无业人员
            case "80": //退离休人员
            case "90": //其他
                zhiYeMC = "不便分类的其他从业人员";
                break;
        }
        return zhiYeMC;
    }

    private String zhiYeDM2261To6565(String daiMa) {
        String zhiYeDM = "Y";
        switch (daiMa) {
            case "11": //国家公务员
                zhiYeDM = "0";
                break;
            case "13": //专业技术人员
                zhiYeDM = "1";
                break;
            case "17": //职员
                zhiYeDM = "3";
                break;
            case "21": //企业管理人员
                zhiYeDM = "0-5";
                break;
            case "24": //工人
                zhiYeDM = "6";
                break;
            case "27": //农民
                zhiYeDM = "5";
                break;
            case "37": //现役军人
                zhiYeDM = "X";
                break;
            case "31": //学生
            case "51": //自由职业者
            case "54": //个体经营者
            case "70": //无业人员
            case "80": //退离休人员
            case "90": //其他
                zhiYeDM = "Y";
                break;
        }
        return zhiYeDM;
    }

    private String shiFouYouZhi(Object value) {
        if (!Objects.isNull(value)) {
            return String.valueOf(value);
        } else {
            return "-" ;
        }
    }
    
    private JianKangWT createJianKangWT(XinShengEJTFSVo info){
        JianKangWT data = new JianKangWT();
    	data.setYunZhou(null != info.getCHUSHENGYZ()? String.valueOf(info.getCHUSHENGYZ()):"");
    	data.setZhuCanJGMC(info.getZHUCHANJGMC());
        data.setMuQingRCHBZ(info.getMUQINRSQHBQKDM());
        data.setMoCiFMFS(new CodeObject(info.getCHUSHENGQKDM(),info.getCHUSHENGQKMC()));
        data.setXinShengEZXBZ("1".equals(info.getXINSHENGEZXBZ())? "true":"false");
        data.setApgarPingFZ((null == info.getAPGARYFZPF() || "-".equals(info.getAPGARYFZPF()))? "0" : info.getAPGARYFZPF());
        data.setWeiYangFSLB(new CodeObject(info.getWEIYANGFSDM(), info.getWEIYANGFSMC()));
        Integer chiNaiCS = 1;
        if (!StringUtil.isEmpty(info.getCHINAICS())) {
            try{
                String chiNaiCSStr = info.getCHINAICS().contains("-") ?  info.getCHINAICS().split("-")[0]: info.getCHINAICS();
                chiNaiCS = Integer.valueOf(chiNaiCSStr);
            }catch (Exception e){

            }

        }
        data.setMeiTianCNCS(String.valueOf(chiNaiCS));
        Integer chiNaiL = 50;
        if (!StringUtil.isEmpty(info.getCHINAIL())) {
            try{
                String chiNaiLStr = info.getCHINAIL().contains("-") ?  info.getCHINAIL().split("-")[0]: info.getCHINAIL();
                chiNaiL = Integer.valueOf(chiNaiLStr);
            }catch (Exception e){

            }

        }
        data.setMeiTianCNL(String.valueOf(chiNaiCS*chiNaiL));
        data.setOuTuBZ("1".equals(info.getOUTUBZ())? "true":"false");
        data.setXinShengEDBXZ(new CodeObject(String.valueOf(info.getDABIANXZDM()), info.getDABIANXZMC()));
        data.setDaBianCS(info.getDABIANCS());
    	return data;
    }
    
    private JianKangZD createJianKangZD(XinShengEJTFSVo info){
        JianKangZD data = new JianKangZD();
        List<JianKangZDDetail> jianKangZDLB = new ArrayList<>();
        if (!StringUtil.isEmpty(info.getBAOJIANZDLBDM())) {
            List<String> daiMa = new ArrayList<>(Arrays.asList(info.getBAOJIANZDLBDM().split("\\|")));
            for (String str : daiMa) {
                JianKangZDDetail jianKangZDDetail = new JianKangZDDetail();
                CodeObject codeObject = new CodeObject(str, shiPeiBAOJIANZDLBMC(str));
                jianKangZDDetail.setJianKangZDDetail(codeObject);
                jianKangZDLB.add(jianKangZDDetail);
            }
        }
        data.setJianKangZDLB(jianKangZDLB);
    	return data;
    }

    private String shiPeiBAOJIANZDLBMC(String str) {
        String bAOJIANZDLBMC = "其他";
        switch (str) {
            case "1":
                bAOJIANZDLBMC = "科学喂养";
                break;
            case "2":
                bAOJIANZDLBMC = "合理膳食";
                break;
            case "3":
                bAOJIANZDLBMC = "生长发育";
                break;
            case "4":
                bAOJIANZDLBMC = "疾病预防";
                break;
            case "5":
                bAOJIANZDLBMC = "预防意外伤害";
                break;
            case "6":
                bAOJIANZDLBMC = "口腔保健";
                break;
            case "9":
                bAOJIANZDLBMC = "其他";
                break;
        }
        return bAOJIANZDLBMC;
    }

    private ShengMingTZ createShengMingTZ(XinShengEJTFSVo info){
        ShengMingTZ data = new ShengMingTZ();
        data.setTiWen(null != info.getTIWEN()? info.getTIWEN().toString():"-");
        data.setMaiLv(null != info.getMAILV()? String.valueOf(info.getMAILV()) : "");
        data.setHuXiPL(null != info.getHUXIPL()? String.valueOf(info.getHUXIPL()) : "");
        data.setShenChang(null != info.getCHUSHENGSC()? String.valueOf(info.getCHUSHENGSC()) : "");
        data.setTiZhong(null != info.getMUQIANTZ()? String.valueOf(info.getMUQIANTZ().setScale(1, RoundingMode.HALF_UP)) : "");
        data.setChuShengTZ(null != info.getCHUSHENGTZ()? String.valueOf(info.getCHUSHENGTZ()) : "");
        data.setChuShengSC(null != info.getCHUSHENGSC()? String.valueOf(info.getCHUSHENGSC()) : "");
    	return data;
    }
    
    private ShengZhiQi createShengZhiQi(XinShengEJTFSVo info){
    	ShengZhiQi data = new ShengZhiQi();
    	data.setShengZhiQJCYCBZ("2".equals(info.getWAISHENGZQYCBZ())? "true":"false");
    	data.setShengZhiQJCYCJGMS(StringUtil.isEmpty(info.getWAISHENGZQYCXS())? "-":info.getWAISHENGZQYCXS());
    	return data;
    }
    
    private XiaCiSFAP createXiaCiSF(XinShengEJTFSVo info){
        XiaCiSFAP data = new XiaCiSFAP();
    	data.setXiaCiSFSJ(CDAUtil.toD8(info.getXIACISFRQ()));
        data.setXiaCiSFDD(StringUtil.isEmpty(info.getXIACISFDD())? "未知":info.getXIACISFDD());
    	return data;
    }
    
    private ZhuanZhenJY createZhuanZhenJY(XinShengEJTFSVo info){
        ZhuanZhenJY data = new ZhuanZhenJY();
    	data.setZhuanZhenBZ("1".equals(info.getZHUANZHENBZ())?"true":"false");
    	data.setZhuanZhenYY(info.getZHUANZHENYY());
    	data.setZhuanRuJG(info.getZHUANRUYLJGMC());
    	data.setZhuanRuKS(info.getKESHIMC());
    	return data;
    }


    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData,XinShengEJTFSVo info, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD8(info.getJILURQ()));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setSetId(String.valueOf(info.getId()));
        clinicalDocumentData.setAuthor(createChuangJianZ(info, hospital));
        clinicalDocumentData.setVersionNumber("1.0");
        clinicalDocumentData.setPatientRole(createPatientRole(info));
        return clinicalDocumentData;
    }
    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setAddr(hospital.getAddress());
        custodian.setTelecom(hospital.getPhone());
        return custodian;
    }
    private String hospitalIdCL(String hospitalId) {
        String subStr = "";
        if (hospitalId.length() < 10) {
            subStr = String.format("%-10s", subStr).replace(' ', '0'); // 不足10位则补0
        } else {
            subStr = hospitalId.substring(0, 10);// 截取前10位
        }
        return subStr;
    }

    PatientRole createPatientRole(XinShengEJTFSVo info) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(info.getGERENDABS()));
        patientRole.setPatient(createPatient(info));
        return patientRole;
    }


    Patient createPatient(XinShengEJTFSVo info) {
        Patient patient = new Patient();
        patient.setName(info.getERTONGXM());
        patient.setId(info.getSHENFENZH());
        return patient;
    }
}
